package com.controller;

import com.base.AjaxRequest;
import com.base.AjaxResult;
import com.base.BaseController;
import com.base.MetaInfo;
import com.bean.WebsiteContent;
import com.bean.anno.LogAnno;
import com.bean.system.MenuInfo;
import com.github.pagehelper.PageHelper;
import com.mapper.repository.mysql.SysMenuInfoRepository;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Log4j2
@RestController
@RequestMapping("sys")
public class SystemController extends BaseController {


    @Autowired
    private SysMenuInfoRepository sysMenuInfoRepository;

    @GetMapping("menus")
    public AjaxResult menus(){
        List<MenuInfo> menuInfos = sysMenuInfoRepository.findAllByIsdelOrderBySequAsc("N");
        return success("获取成功",menuInfos);
    }


    //获取菜单列表  菜单管理
    @PostMapping("menu/list")
    public AjaxResult list(){
        List<MenuInfo> list = sysMenuInfoRepository.findAllByOrderBySequAsc();
        return success("查询成功",list);
    }

    //菜单名称 菜单链接 不能已经存在
    @PostMapping("menu/add")
    public AjaxResult add(@RequestBody MenuInfo menuInfo){
        int count = sysMenuInfoRepository.countByNameAndAndHref(menuInfo.getName(),menuInfo.getHref());
        if(count>0){
            return error("新增的菜单已存在");
        }
        String id = UUID.randomUUID().toString();
        menuInfo.setId(id);
        menuInfo.setCreatetime(LocalDateTime.now());
        menuInfo.setIsdel("N");
        sysMenuInfoRepository.save(menuInfo);
        return success("新增成功");
    }


    @PostMapping("menu/del")
    public AjaxResult del(@RequestBody MenuInfo menuInfo){
        menuInfo.setIsdel("Y");
        sysMenuInfoRepository.save(menuInfo);
        return success("删除成功");
    }

    @PostMapping("menu/update")
    public AjaxResult update(@RequestBody MenuInfo menuInfo){
        int count = sysMenuInfoRepository.countByNameAndAndHrefAndIdIsNot(menuInfo.getName(),menuInfo.getHref(),menuInfo.getId());
        if(count>0){
            return  error("更新的菜单已存在");
        }
        sysMenuInfoRepository.save(menuInfo);
        return success("更新成功");
    }

    @GetMapping("menu/{id}")
    public AjaxResult getOne(@PathVariable("id") String id){
        MenuInfo menuInfo = sysMenuInfoRepository.getById(id);
        return success("查询成功",menuInfo);
    }
}
